/**
 * 回款
 * @author mengyb
 * @date 2019/08/09
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_param = {},
        cols = [[
            {type:'checkbox',fixed: 'left'},
            {field: 'NAME_AZP', width: 100, title: '归属法人', fixed: 'left',sort: true}
            , {field: 'AZP02', width: 120, title: 'ERP营运中心'}
            , {field: 'GWEODOCNO', width: 160, title: '回款单号',search: 'default', searchOpts: {icon: true}}
            , {field: 'GWEODOCDATE', width: 100, title: '单据日期',search: 'default', searchOpts: {type: 'date'}}
            , {field: 'GWAKL004', width: 100, title: '客户',search: 'default', searchOpts: {icon: true}}
            , {field: 'GWEOSEQ', width: 100, title: '项次'}
            , {field: 'GWEO001', width: 100, title: '款别',filterDatas: app.datas_('crm_gwei009_gweo001')}
            , {field: 'GWEO002', width: 100, title: '币别'}
            , {field: 'GWEO003', width: 100, title: '汇率'}
            , {field: 'GWEO004', width: 100, title: '原币金额'}
            , {field: 'GWEO005', width: 100, title: '本币金额'}
            , {field: 'NAME_ORG', width: 100, title: '部门'}
            , {field: 'NAME_USER', width: 100, title: '人员',search: 'default', searchOpts: {icon: true}}
            , {field: 'GWEO009', width: 100, title: '备注'}
            , {field: 'GWEOACTI', width: 100, title: '状态',
                filterDatas: app.datas_('crm_gwei009_gweoacti').concat({code:'',value:'全部'}),
                search: 'default',
                searchOpts: {type: 'select', default: ''}}
            ,
            {
                width: 60,
                title: '',
                toolbar: '#script-gwei009setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwei009SettingAction,
        moduleName = '回款'
    ;

    app.newModule({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwei009',
                _searchs: [{
                    id: 'gwei009', tableId: 'gwei009_table', datas:  cols[0]
                }],
                _loadTplsFull:[
                    {dom:'gwei009_edit',tpl:'gwei009_edit'},
                    {dom:'common_open_window',tpl:'common/_open_window'},
                    // {dom:'common_open_kh_gwei009_search',tpl:'common/_open_kh_gwei009_search'},
                    // {dom:'common_open_ywy_gwei009_search',tpl:'common/_open_ywy_gwei009_search'},
                ]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwei009_table'),
                    url: g_ctxPath + 'crm/gwei009/getList',
                    exportsUrl: g_ctxPath + 'crm/gwei009/getList',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    method: 'post',
                    id: 'gwei009_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: ''
                });

                layui.laytpl(app.getByid(doc, 'script-gwei009setting-action').innerHTML).render({}, function (string) {
                    gwei009SettingAction = string;
                });

                var tipsIndex = null;

                //监听行工具栏事件，弹出编辑与删除按钮 lay-filter指定事件
                table.on('tool(gwei009_table)', function (obj) {

                    layer.tips(gwei009SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        if(obj.data.GWEOACTI != 'N'  ){
                                            app.showWarning("已确认的资料无法修改！");
                                            return ;
                                        }
                                        self._openTplFull({
                                            tag: 'gwei009_edit',
                                            tagData:{
                                                moduleName:moduleName,
                                                obj:obj,
                                                isAdd:false,
                                                isDetail:false
                                            },
                                            index: 0,
                                            openOpts:{
                                                title: moduleName + '信息编辑',
                                                minmax:true
                                            }
                                        });
                                        break;
                                    case '1':
                                        if(obj.data.GWEOACTI != 'N'  ){
                                            app.showWarning("已确认的资料无法删除！");
                                            return ;
                                        }
                                        self.delRow(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwei009_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwei009_edit',
                        tagData: {
                            moduleName:moduleName,
                            obj:obj,
                            isDetail:true
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '详情',
                            minmax:true
                        }
                    });
                });

                //监听头工具条事件
                table.on('toolbar(gwei009_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwei009_edit',
                                tagData: {
                                    moduleName:moduleName,
                                    isAdd: true
                                },
                                index: 0,
                                openOpts: {
                                    title: '新增'+moduleName,
                                    minmax:true
                                }
                            });
                            break;
                        case 'del':
                            self.delRows();
                            break;
                        case 'confirm':
                            self.confirm();
                            break;
                        case 'confirmCancel':
                            self.unConfirm();
                            break;

                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwei009_table', where: table_param});
        },

        delRow: function (obj) {
            app.confirm({
                msg:'确认删除该条数据吗？',
                ok:function () {
                    app.ajax({
                        xurl: 'crm/gwei009/delRow',
                        xdata: obj.data,
                        loading: true,
                        okmsg: moduleName +'信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            })
        },

        delRows: function(){
            var checkStatus = table.checkStatus('gwei009_table');
            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if(v.GWEOACTI == 'N'){
                        var _temp = app.fields_('base_gwei009');
                        _temp.GWEOSEQ = v.GWEOSEQ;
                        _temp.GWEODOCNO = v.GWEODOCNO;
                        _xdata.push(_temp);
                    }else{
                        app.showWarning("已确认的资料无法删除！");
                        flag = false;
                        return ;
                    }

                });
                if(flag){
                    app.confirm({
                        msg:'是否删除所选择的数据？',
                        ok: function () {
                            app.ajax({
                                xurl:'crm/gwei009/delRows',
                                xdata:_xdata,
                                toJsonStr:true,
                                okmsg:moduleName+'信息删除成功',
                                loading:true,
                                cb: function (result) {
                                    if (result.ok) {
                                        self.reloadTableCurr();
                                    }
                                }
                            })
                        }
                    });
                }
            }else{
                app.showMessage('请先选择待删除的数据');
            }
        },

        //确认
        confirm: function () {
            var checkStatus = table.checkStatus('gwei009_table');
            if (checkStatus.data.length > 0) {
                var _xdata=[];
                var flag=true;//是否满足确认条件
                checkStatus.data.forEach(function (v) {
                    if (v.GWEOACTI == 'N') {
                        var _xtemp=app.fields_('base_gwei009');
                        _xtemp.GWEODOCNO=v.GWEODOCNO;
                        // _xtemp.GWEOSEQ=v.GWEOSEQ;
                        _xdata.push(_xtemp);
                    }else {
                        flag=false;
                        return;
                    }
                });
                if(flag){//满足确认条件
                    app.ajax({
                        xurl:'crm/gwei009/confirm',
                        xdata:_xdata,
                        loading:true,
                        toJsonStr:true,
                        confirm: {msg: "是否确认选择的数据？"},
                        okmsg: moduleName + '信息确认成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }

                        }
                    })
                }else {//不满足去人条件
                    app.showWarning('不可重复确认');
                }
            }else{
                app.showWarning('请先选择待确认的数据');
            }
        },
        //取消确认
        unConfirm: function () {
            var checkStatus = table.checkStatus('gwei009_table');
            if (checkStatus.data.length > 0) {
                var _xdata=[];
                var flag=true;//是否满足取消确认条件
                checkStatus.data.forEach(function (v) {
                    if (v.GWEOACTI == 'Y') {
                        var _xtemp=app.fields_('base_gwei009');
                        _xtemp.GWEODOCNO=v.GWEODOCNO;
                        // _xtemp.GWEOSEQ=v.GWEOSEQ;
                        _xdata.push(_xtemp);
                    }else {
                        flag=false;
                        return;
                    }
                });
                if (flag) {//满足取消确认条件
                    app.ajax({
                        xurl: 'crm/gwei009/unConfirm',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        confirm: {msg: "是否取消确认选择的数据？"},
                        okmsg: moduleName + '信息取消确认成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                } else {//不满足取消审核条件
                    app.showWarning('不可重复取消确认');
                }
            }else{//未选择数据
                app.showWarning('请先选择待取消确认的数据');
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwei009',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWEODOCNO') {//回款单号开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'cq_gweo_1',
                                            columnList: [
                                                {label: '回款单号', prop: 'GWEODOCNO', width: '150'},
                                                {label: '单据日期', prop: 'GWEODOCDATE', width: '150'}
                                            ],
                                            // url: 'crm/gwei009/listGweoPageC',
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWEODOCNO = data.GWEODOCNO;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择回款单号',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'GWAKL004') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'cq_gwak001_3',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWEO006', width: '150'},
                                                {label: '客户名称', prop: 'GWAKL004', width: '150'}
                                            ],
                                            // url: 'crm/gwei009/listGwakPageC',
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWAKL004 = data.GWEO006;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            }else if (field == 'NAME_USER') {//业务员开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'cq_sysuser_id_2',
                                            columnList: [
                                                {label: '业务员ID', prop: 'GWEO007', width: '150'},
                                                {label: '业务员名称', prop: 'NAME', width: '150'}
                                            ],
                                            // url: 'crm/gwei009/listUserPageC',
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.NAME_USER = data.GWEO007;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择业务员',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },

        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });

        }
    });

});
